package com.example.news.controller;

import com.example.news.pojo.form.AddUserRoleForm;
import com.example.news.pojo.form.ChangeUserInfoForm;
import com.example.news.pojo.form.RegisterForm;
import com.example.news.pojo.form.UserPageQuery;
import com.example.news.pojo.vo.UserInfoVo;
import com.example.news.service.UserService;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "2.用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/info/{id}")
    @Operation(summary = "根据id获取用户信息")
    public Result<UserInfoVo> getUserByID(@PathVariable Integer id) {
        return userService.getOtherInfo(id);
    }

    @GetMapping("/mine/info")
    @Operation(summary = "获取自己用户信息")
    public Result<UserInfoVo> getUserByID(@RequestHeader String token) {
        return userService.getMineInfo(token);
    }

    @GetMapping("/infoList")
    @Operation(summary = "用户信息分页")
    public PageResult<UserInfoVo> getUserList(@ParameterObject UserPageQuery userPageQuery,@RequestHeader String token) {
        return userService.getUserInfoList(userPageQuery);
    }

    @PostMapping("/info")
    @Operation(summary = "管理员添加用户")
    public Result<String> add(@RequestBody RegisterForm user, @RequestHeader String token) {
        return userService.addUser(user);
    }

    @PutMapping("/info/update")
    @Operation(summary = "用户资料修改")
    public Result<String> updateUser(@RequestBody ChangeUserInfoForm changeUserInfoForm) {
        return userService.updateUserInfo(changeUserInfoForm);
    }

    @PutMapping("/status/{status}/{id}")
    @Operation(summary = "管理员修改用户状态")
    public Result<String> updateUser(@PathVariable Integer id,
                                     @Parameter(description = "用户状态(1:启用;0:禁用)") @PathVariable Integer status,@RequestHeader String token) {
        return userService.updateUserStatus(id, status);
    }

    @DeleteMapping("/info/{id}")
    @Operation(summary = "管理员删除用户")
    public Result<String> updateUser(@PathVariable Integer id,@RequestHeader String token) {
        return userService.deleteUser(id);
    }

    @DeleteMapping("/mine/info")
    @Operation(summary = "用户自己删除")
    public Result<String> updateUser(@RequestHeader String token) {
        return userService.deleteMine(token);
    }

    @PostMapping("/role")
    @Operation(summary = "为用户添加角色")
    public Result<String> addUserRole(@RequestBody AddUserRoleForm addUserRoleForm,@RequestHeader String token) {
        return userService.addUserRole(addUserRoleForm);
    }

}
